home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database Designers / Rational Rose 2000 / Rational Setup.EXE / common / lib / Win32 / OLE / Const.pm next >
Text File  |  1998-11-15  |  6KB  |  200 lines

  1. # The documentation is at the __END__
  2.  
  3. package Win32::OLE::Const;
  4.  
  5. use strict;
  6. use Carp;
  7. use Win32::OLE;
  8. use Win32::Registry;
  9.  
  10. sub import {
  11.     my ($self,$name,$major,$minor,$language,$codepage) = @_;
  12.     return unless defined($name) && $name !~ /^\s*$/;
  13.     $self->Load($name,$major,$minor,$language,$codepage,scalar caller);
  14. }
  15.  
  16. sub Load {
  17.     my ($pack,$name,$major,$minor,$language,$codepage,$caller) = @_;
  18.     undef $minor unless defined $major;
  19.  
  20.     return _Load($name,undef,undef,undef,undef,undef,undef)
  21.       if UNIVERSAL::isa($name,'Win32::OLE');
  22.  
  23.     unless (defined($name) && $name !~ /^\s*$/) {
  24.     carp "Win32::OLE::Const->Load: No or invalid type library name";
  25.     return;
  26.     }
  27.  
  28.     my ($hTypelib,$hClsid,$hVersion,$hLangid);
  29.     my @found;
  30.  
  31.     unless ($main::HKEY_CLASSES_ROOT->Create('TypeLib',$hTypelib)) {
  32.     carp "Cannot access HKEY_CLASSES_ROOT\\Typelib";
  33.     return;
  34.     }
  35.  
  36.     my $Clsids = [];
  37.     $hTypelib->GetKeys($Clsids);
  38.  
  39.     foreach my $clsid (@$Clsids) {
  40.     $hTypelib->Create($clsid,$hClsid);
  41.     my $Versions = [];
  42.     $hClsid->GetKeys($Versions);
  43.     foreach my $version (@$Versions) {
  44.         my $value;
  45.         next unless $hClsid->QueryValue($version,$value);
  46.         next unless $value =~ /^$name/;
  47.  
  48.         my ($maj,$min) = ($version =~ /^(\d+)\.(\d+)$/);
  49.         next unless defined $min;
  50.         next if defined($major) && $maj != $major;
  51.         next if defined($minor) && $min < $minor;
  52.  
  53.         $hClsid->Create($version,$hVersion);
  54.         my $Langids = [];
  55.         $hVersion->GetKeys($Langids);
  56.         foreach my $langid (@$Langids) {
  57.         next unless $langid =~ /^\d+$/;
  58.         next if defined($language) && $language != $langid;
  59.         $hVersion->Create($langid,$hLangid);
  60.         my $filename;
  61.         $hLangid->QueryValue('win32',$filename);
  62.         $hLangid->Close;
  63.         push @found, [$clsid,$maj,$min,$langid,$filename];
  64.         }
  65.         $hVersion->Close;
  66.     }
  67.     $hClsid->Close;
  68.     }
  69.     $hTypelib->Close;
  70.  
  71.     unless (@found) {
  72.     carp "No type library matching \"$name\" found";
  73.     return;
  74.     }
  75.  
  76.     @found = sort {
  77.     # Prefer greater version number
  78.     my $res = $b->[1] <=> $a->[1];
  79.     $res = $b->[2] <=> $a->[2] if $res == 0;
  80.     # Prefer default language for equal version numbers
  81.     $res = -1 if $res == 0 && $a->[3] == 0;
  82.     $res =  1 if $res == 0 && $b->[3] == 0;
  83.     return $res;
  84.     } @found;
  85.  
  86.     #printf "Loading %s\n", join(' ', @{$found[0]});
  87.     return _Load(@{$found[0]},$codepage,$caller);
  88. }
  89.  
  90. 1;
  91.  
  92. __END__
  93.  
  94. =head1 NAME
  95.  
  96. Win32::OLE::Const - Extract constant definitions from TypeLib
  97.  
  98. =head1 SYNOPSIS
  99.  
  100.     use Win32::OLE::Const 'Microsoft Excel';
  101.     printf "xlMarkerStyleDot = %d\n", xlMarkerStyleDot;
  102.  
  103.     my $wd = Win32::OLE::Const->Load("Microsoft Word 8\\.0 Object Library");
  104.     foreach my $key (keys %$wd) {
  105.         printf "$key = %s\n", $wd->{$key};
  106.     }
  107.  
  108. =head1 DESCRIPTION
  109.  
  110. This modules makes all constants from a registered OLE type library
  111. available to the Perl program. The constant definitions can be
  112. imported as functions, providing compile time name checking.
  113. Alternatively the constants can be returned in a hash reference
  114. which avoids defining lots of functions of unknown names.
  115.  
  116. =head2 Functions/Methods
  117.  
  118. =over 4
  119.  
  120. =item use Win32::OLE::Const
  121.  
  122. The C<use> statement can be used to directly import the constant names
  123. and values into the users namespace.
  124.  
  125.     use Win32::OLE::Const (TYPELIB,MAJOR,MINOR,LANGUAGE);
  126.  
  127. The TYPELIB argument specifies a regular expression for searching
  128. through the registry for the type library. Note that this argument is
  129. implicitly prefixed with C<^> to speed up matches in the most common
  130. cases. Use a typelib name like ".*Excel" to match anywhere within the
  131. description. TYPELIB is the only required argument.
  132.  
  133. The MAJOR and MINOR arguments specify the requested version of
  134. the type specification. If the MAJOR argument is used then only
  135. typelibs with exactly this major version number will be matched. The
  136. MINOR argument however specifies the minimum acceptable minor version.
  137. MINOR is ignored if MAJOR is undefined.
  138.  
  139. If the LANGUAGE argument is used then only typelibs with exactly this
  140. language id will be matched.
  141.  
  142. The module will select the typelib with the highest version number
  143. satisfying the request. If no language id is specified then a the default
  144. language (0) will be preferred over the others.
  145.  
  146. Note that only constants with valid Perl variable names will be exported,
  147. i.e. names matching this regexp: C</^[a-zA-Z_][a-zA-Z0-9_]*$/>.
  148.  
  149. =item Win32::OLE::Const->Load
  150.  
  151. The Win32::OLE::Const->Load method returns a reference to a hash of
  152. constant definitions.
  153.  
  154.     my $const = Win32::OLE::Const->Load(TYPELIB,MAJOR,MINOR,LANGUAGE);
  155.  
  156. The parameters are the same as for the C<use> case.
  157.  
  158. This method is generally preferrable when the typelib uses a non-english
  159. language and the constant names contain locale specific characters not
  160. allowed in Perl variable names.
  161.  
  162. Another advantage is that all available constants can now be enumerated.
  163.  
  164. The load method also accepts an OLE object as a parameter. In this case
  165. the OLE object is queried about its containing type library and no registry
  166. search is done at all. Interestingly this seems to be slower.
  167.  
  168. =back
  169.  
  170. =head1 EXAMPLES
  171.  
  172. The first example imports all Excel constants names into the main namespace
  173. and prints the value of xlMarkerStyleDot (-4118).
  174.  
  175.     use Win32::OLE::Const ('Microsoft Excel 8.0 Object Library');
  176.     print "xlMarkerStyleDot = %d\n", xlMarkerStyleDot;
  177.  
  178. The second example returns all Word constants in a hash ref.
  179.  
  180.     use Win32::OLE::Const;
  181.     my $wd = Win32::OLE::Const->Load("Microsoft Word 8.0 Object Library");
  182.     foreach my $key (keys %$wd) {
  183.         printf "$key = %s\n", $wd->{$key};
  184.     }
  185.     printf "wdGreen = %s\n", $wd->{wdGreen};
  186.  
  187. The last example uses an OLE object to specify the type library:
  188.  
  189.     use Win32::OLE;
  190.     use Win32::OLE::Const;
  191.     my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
  192.     my $xl = Win32::OLE::Const->Load($Excel);
  193.  
  194.  
  195. =head1 AUTHORS/COPYRIGHT
  196.  
  197. This module is part of the Win32::OLE distribution.
  198.  
  199. =cut
  200.